package com.hanbang.taitaigou.ui.activity.gouwuche;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.model.GetOrderByID;
import com.hanbang.taitaigou.model.GoodListData;
import com.hanbang.taitaigou.pay.alipay.Alipay;
import com.hanbang.taitaigou.pay.wxpay.WxPay;
import com.hanbang.taitaigou.ui.activity.aboutme.coupon.AboutCouponActivity;
import com.hanbang.taitaigou.ui.activity.base.BaseActivity;
import com.hanbang.taitaigou.ui.activity.taitaigou.ProductDetailsActivity;
import com.hanbang.taitaigou.ui.widget.ListViewForScrollView;
import com.hanbang.taitaigou.utils.http.BitmapUtil;
import com.hanbang.taitaigou.utils.http.HttpCallBack;
import com.hanbang.taitaigou.utils.http.HttpLocalUtils;
import com.hanbang.taitaigou.utils.http.HttpRequestParams;
import com.hanbang.taitaigou.utils.http.JsonHelper;
import com.hanbang.taitaigou.utils.ui.ToastUtils;
import com.hanbang.taitaigou.utils.ui.UiUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanxin on 2016/3/26.
 */
public class AboutMyOrderCommitActivity extends BaseActivity {
    //选择支付方式
    private LinearLayout ll_zfb, ll_wx, ll_g20, ll_zhye;
    private CheckBox cb_zfb, cb_wx, cb_g20, cb_zhye;
    //商品列表
    private ListViewForScrollView lvfsv_listview;
    private GetOrderByID getOrderByID = null;
    private List<GoodListData> list = new ArrayList<GoodListData>();
    private ListViewAdapyer listViewAdapyer;

    //总价
    private TextView tv_total;
    //实价付款价格
    private TextView tv_net;
    //总价
    private double bb;
    //优惠券的价格
    private TextView tv_yhq_money;
    private LinearLayout ll_yhq;
    private double money;

    //结算
    private Button button;

    private int orderId = -1;

    private  String orderNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about_my_order_commit);
        initView();
        setOnClickListener();
        getOrderByID();
    }

    /*
     * 设置点击事件
     */
    private void setOnClickListener() {
        ll_zfb.setOnClickListener(onClickListener);
        ll_wx.setOnClickListener(onClickListener);
        ll_g20.setOnClickListener(onClickListener);
        ll_zhye.setOnClickListener(onClickListener);
        ll_yhq.setOnClickListener(onClickListener);

        button.setOnClickListener(onClickListener);
    }

    @SuppressWarnings("unused")
    private View.OnClickListener onClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ll_zfb:
                    cb_zfb.setBackgroundResource(R.mipmap.check_box_ok);
                    cb_zfb.setChecked(true);
                    cb_wx.setBackgroundResource(R.mipmap.check_box_no);
                    cb_wx.setChecked(false);
                    cb_g20.setBackgroundResource(R.mipmap.check_box_no);
                    cb_g20.setChecked(false);
                    cb_zhye.setBackgroundResource(R.mipmap.check_box_no);
                    cb_zhye.setChecked(false);
                    break;
                case R.id.ll_wx:
                    cb_zfb.setBackgroundResource(R.mipmap.check_box_no);
                    cb_zfb.setChecked(false);
                    cb_wx.setBackgroundResource(R.mipmap.check_box_ok);
                    cb_wx.setChecked(true);
                    cb_g20.setBackgroundResource(R.mipmap.check_box_no);
                    cb_g20.setChecked(false);
                    cb_zhye.setBackgroundResource(R.mipmap.check_box_no);
                    cb_zhye.setChecked(false);
                    break;
                case R.id.ll_g20:
                    cb_zfb.setBackgroundResource(R.mipmap.check_box_no);
                    cb_zfb.setChecked(false);
                    cb_wx.setBackgroundResource(R.mipmap.check_box_no);
                    cb_wx.setChecked(false);
                    cb_g20.setBackgroundResource(R.mipmap.check_box_ok);
                    cb_g20.setChecked(true);
                    cb_zhye.setBackgroundResource(R.mipmap.check_box_no);
                    cb_zhye.setChecked(false);
                    break;
                case R.id.ll_zhye:
                    cb_zfb.setBackgroundResource(R.mipmap.check_box_no);
                    cb_zfb.setChecked(false);
                    cb_wx.setBackgroundResource(R.mipmap.check_box_no);
                    cb_wx.setChecked(false);
                    cb_g20.setBackgroundResource(R.mipmap.check_box_no);
                    cb_g20.setChecked(false);
                    cb_zhye.setBackgroundResource(R.mipmap.check_box_ok);
                    cb_zhye.setChecked(true);
                    break;
                case R.id.ll_yhq:
                    Intent intent = new Intent(AboutMyOrderCommitActivity.this, AboutCouponActivity.class);
                    intent.putExtra("type", 1);
                    startActivityForResult(intent, 999);
                    break;

                case R.id.button:
                    if(cb_zfb.isChecked()){
                        new Alipay(AboutMyOrderCommitActivity.this, new Alipay.OnAlipayCallback() {
                            @Override
                            public void onCallback(boolean isSuccess, String out_trade_no, String subject, String body, String total_fee) {
//                                LogUtils.e("Alipay", isSuccess + "  " + out_trade_no + "   " + subject + "   " + body + "    " + total_fee);
                                if(isSuccess){
                                    Intent intent = new Intent();
                                    intent.setClass(AboutMyOrderCommitActivity.this,PaymentSuccessActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            }
                        }).alipay(list.get(0).getGname(),"太太购",Double.parseDouble(tv_net.getText().toString().substring(1)),orderNo);
                        break;
                    }else if(cb_wx.isChecked()){
                        new WxPay(AboutMyOrderCommitActivity.this, new WxPay.OnFinshCallback() {
                            @Override
                            public void onFinsh(boolean isSuccess) {
                                ToastUtils.show(AboutMyOrderCommitActivity.this, "支付失败", 1);
                                finish();
                            }
                        }).orderPayByApp(orderNo,getOrderByID.getId()+","+userData.getMobile()+","+userData.getUser_id());

                    }else{
                        ToastUtils.show(AboutMyOrderCommitActivity.this,"目前只支持支付宝支付...", Toast.LENGTH_LONG);
                    }
                    break;
                default:
                    break;
            }
        }

    };


    /**
     * 优惠券的回调
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 999 && resultCode == RESULT_OK) {
            int id = data.getIntExtra("id", -1);
            money = data.getIntExtra("qian", -1);
            HttpRequestParams httpRequestParams = new HttpRequestParams("UpdateOrderStatus");
            httpRequestParams.addUserId(userData);
            httpRequestParams.addBodyParameter("edit_type", "update_total_price");
            httpRequestParams.addBodyParameter("order_id", String.valueOf(id));
            httpRequestParams.addBodyParameter("Money", String.valueOf(money));
            httpManager.post(httpRequestParams, new HttpCallBack<String>(this, "正在获取...") {
                @Override
                public void onSuccess(String result) {
                    super.onSuccess(result);
                    tv_yhq_money.setText("已优惠¥" + money + "元");
                    tv_net.setText("¥" + ProductDetailsActivity.sswr(bb - money));
                }
            });
        }
    }



    /*
         * 初始化view
         */
    private void initView() {
        // 设置顶部的标题
        setTop(null, 0, getString(R.string.top_order_commit), null, 0);
        setBackArrows(findViewById(R.id.top_bar_left_iv));

        ll_zfb = (LinearLayout) findViewById(R.id.ll_zfb);
        ll_wx = (LinearLayout) findViewById(R.id.ll_wx);
        ll_g20 = (LinearLayout) findViewById(R.id.ll_g20);
        ll_zhye = (LinearLayout) findViewById(R.id.ll_zhye);

        cb_zfb = (CheckBox) findViewById(R.id.cb_zfb);
        cb_wx = (CheckBox) findViewById(R.id.cb_wx);
        cb_g20 = (CheckBox) findViewById(R.id.cb_g20);
        cb_zhye = (CheckBox) findViewById(R.id.cb_zhye);

        lvfsv_listview = (ListViewForScrollView) findViewById(R.id.lvfsv_listview);
        tv_total = (TextView) findViewById(R.id.tv_total);
        tv_net = (TextView) findViewById(R.id.tv_net);
        tv_yhq_money = (TextView) findViewById(R.id.tv_yhq_money);
        ll_yhq = (LinearLayout) findViewById(R.id.ll_yhq);

        button = (Button)findViewById(R.id.button);

        listViewAdapyer = new ListViewAdapyer();
        lvfsv_listview.setAdapter(listViewAdapyer);

    }

    /**
     * 获取订单信息
     */
    private void getOrderByID() {
        Intent intent = getIntent();
        orderId = intent.getIntExtra("order_id", orderId);
        bb = intent.getDoubleExtra("zongjia", bb);
        orderNo = intent.getStringExtra("order_no");
        HttpRequestParams httpRequestParams = new HttpRequestParams("GetOrderByID");
        httpRequestParams.addUserId(userData);
        httpRequestParams.addBodyParameter("order_id", String.valueOf(orderId));
        httpManager.post(httpRequestParams, new HttpCallBack<String>(this, "正在加载...") {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    getOrderByID = JsonHelper.parseObject(jsonObject, GetOrderByID.class);
                    orderNo = jsonObject.getString("order_no");
                    JSONArray resultArray = jsonObject.getJSONArray("goods_detail");
//                    JSONArray js = (JSONArray) getOrderByID.getGoods_detail();
                    if (getOrderByID != null) {
                        list.addAll(JsonHelper.parseCollection(resultArray, ArrayList.class, GoodListData.class));
                        tv_total.setText("¥" + ProductDetailsActivity.sswr(bb));
                        tv_net.setText("¥" + ProductDetailsActivity.sswr(bb));
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    class ListViewAdapyer extends BaseAdapter {

        @Override
        public int getCount() {

            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, final ViewGroup parent) {

            // 常见的优化ViewHolder
            final ViewHolder viewHolder;
            if (null == convertView) {
                viewHolder = new ViewHolder();
                convertView = UiUtils.getInflaterView(AboutMyOrderCommitActivity.this, R.layout.item_my_order_commit);
                viewHolder.iv_photo = (ImageView) convertView.findViewById(R.id.iv_photo);
                viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                viewHolder.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //获取数据
            imageManager.bind(viewHolder.iv_photo, HttpLocalUtils.getHttpFileUrl(list.get(position).getImg_url()), BitmapUtil.getImageOptionsBuilder().build());
            viewHolder.tv_name.setText(list.get(position).getGname());
            viewHolder.tv_number.setText("x"+String.valueOf(list.get(position).getQuantity()));
            return convertView;
        }

        class ViewHolder {
            private ImageView iv_photo;
            private TextView tv_name;
            private TextView tv_number;
        }
    }

}
